<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    var p = new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve(1);
        }, 2000)
    })

    // p.then().then().then().then(res => {
    //     console.log("fulfilled", res);
    // }, err => {
    //     console.log("rejected", err);
    // })


    // p.then(res => {
    //     return res;
    // }, err => {
    //     throw err;
    // }).then(res => {
    //     return res;
    // }, err => {
    //     throw err;
    // }).then(res => {
    //     return res;
    // }, err => {
    //     throw err;
    // }).then(res => {
    //     console.log("fulfilled", res);
    // }, err => {
    //     console.log("rejected", err);
    // })


    // 2. 如果then()中指定成功,失败时的回调函数,  先执行回调函数,等回调函数执行完毕(不出错), 后续的Promise对象也会变为已成功(此时回调函数的结果会作为后续的Promise实例的结果存储)
    // 成功就走then  失败走catch (catch可以捕获整个链式操作过程中所有的错误)
    // p.then(function (res) {
    //     console.log("fulfilled", res);
    //     return 2;
    // }).then(function (res) {
    //     console.log("fulfilled", res);
    //     return 3;
    // }).then(function (res) {
    //     console.log("fulfilled", res);
    //     return 4;
    // }).then(function (res) {
    //     console.log("fulfilled", res);
    // }).catch(err => {
    //     console.log("rejected", err);
    // })


    var p = new Promise(function (resolve, reject) {
        setTimeout(function () {
            // resolve(1);
            reject(1);
        }, 2000)
    })

    p.then(function (res) {
        console.log("fulfilled", res);
        return 2;
    }, function (err) {
        console.log("reject", err); // 1
        // return 22;
        throw 22;
    }).then(function (res) {
        console.log("fulfilled", res); // 22
        return 3;
    }).then(function (res) {
        console.log("fulfilled", res);
    }).catch(err => {
        console.log("rejected", err);
    })


</script>

</html>